package com.example.beautymakeup.controller;


import com.example.beautymakeup.domain.Code;
import com.example.beautymakeup.domain.PageBean;
import com.example.beautymakeup.domain.Result;
import com.example.beautymakeup.pojo.Cart;
import com.example.beautymakeup.pojo.Hot;
import com.example.beautymakeup.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private CartService cartService;

    @GetMapping
    public Result get(@RequestHeader("Shop_token") String token) throws Exception {
        PageBean<Cart> pageBean = cartService.get(token);
        if (pageBean != null){
            return new Result(Code.GET_OK,"获取成功",pageBean);
        }
        return new Result(Code.GET_ERR,"获取失败");
    }

    @PostMapping
    public Result add(@RequestHeader("Shop_token") String token,@RequestBody Cart cart) throws Exception {
        boolean add = cartService.add(token, cart);
        if (add ){
            return new Result(Code.SAVE_OK,"新增成功");
        }
        return new Result(Code.SAVE_ERR,"新增失败");
    }

    @DeleteMapping
    public Result delete(@RequestHeader("Shop_token") String token,@RequestBody Cart cart) throws Exception {
        boolean delete = cartService.delete(token, cart);
        if (delete){
            return new Result(Code.DELETE_OK,"删除成功");
        }
        return new Result(Code.DELETE_ERR,"删除失败");
    }
}
